/* -------------------------------------- 

     Cherrypy webapp skeleton frontend

     License: LGPL
     Authors: saaj (mail@saaj.me)

   -------------------------------------- */

/**
 * Note table controller class
 */
qx.Class.define("webapp.controller.Note", {

  extend : webapp.controller.Abstract,

	members : {

    _table       : null,
    _serviceName : "application.service.Note",
    
    
    _reload : function()
    {
      this._table.getTableModel().reloadData();
    },
    
    _create : function()
    {
      var model = new webapp.model.NoteEditor(this._serviceName);
      model.addListener("saved", this._reload, this);
      
      var view  = new webapp.view.NoteEditor();
      qx.core.Init.getApplication().getRoot().add(view);
      view.setModel(model);
      view.load({});
      view.open();
    },
    
    _remove : function(rowIndex)
    {
      dialog.Dialog.confirm(this.tr("Really remove it?"), function(succeed)
      {
        if(succeed)
        {
          var index = this._table.getSelectionModel().getLeadSelectionIndex();
          this._table.getTableModel().removeRow(index);
        }
      }, this);
    },
    
		createWidget : function()
		{
      var view    = new webapp.view.NoteTable();
      this._table = view.getTable();
      
      var dataModel = new webapp.model.RemoteTable();
      dataModel.setServiceName(this._serviceName);
      view.setTableModel(dataModel);
      
      view.getButtons().reload.addListener("execute", this._reload, this);
      view.getButtons().create.addListener("execute", this._create, this);
      view.getButtons().remove.addListener("execute", this._remove, this);


      return view;
		}
    
	}

});